package com.by;

import com.by.config.Configuration;
import com.by.mapper.UserMapper;
import com.by.mapping.MappedStatement;
import com.by.pojo.User;
import com.by.session.DefaultSqlSession;
import com.by.session.SqlSession;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/8 21:17
 * @注释
 */


public class MyBatisBootstrap {
    public static void main(String[] args) throws Exception {
        // 初始化配置
        Configuration configuration = new Configuration();
        configuration.setDriver("com.mysql.cj.jdbc.Driver");
        configuration.setUrl("jdbc:mysql://192.168.41.41:3306/handle_framwork");
        configuration.setUsername("root");
        configuration.setPassword("1357");

        // 注册 SQL 映射
        MappedStatement selectAllUsers = new MappedStatement();
        selectAllUsers.setSql("SELECT * FROM users");
        selectAllUsers.setResultType(User.class);

        MappedStatement selectUserById = new MappedStatement();
        selectUserById.setSql("SELECT * FROM users WHERE id = ?");
        selectUserById.setParameterType(Integer.class);
        selectUserById.setResultType(User.class);

        configuration.addMappedStatement("com.by.mapper.UserMapper.selectAllUsers", selectAllUsers);
        configuration.addMappedStatement("com.by.mapper.UserMapper.selectUserById", selectUserById);

        // 创建 SqlSession
        SqlSession sqlSession = new DefaultSqlSession(configuration);

        // 获取 Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 执行查询
        System.out.println(userMapper.selectAllUsers());
        System.out.println(userMapper.selectUserById(1));
    }
}
